package com.yanqu.road.server.manager.activity.guagifts;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.guagifts.GuaGiftsBroadcastData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.activity.GuaGiftsConfig.GuaGiftsConfig;
import com.yanqu.road.logic.activity.qixi.QixiConfig;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.GuaGiftsProto;
import com.yanqu.road.pb.activity.QixiProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossGuaGiftsMgr extends TempCrossMgr {
    private static Map<Integer, CrossGuaGiftsActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean removeExpireData() throws Exception {
        int currentSecond = DateHelper.getCurrentSecond();
        List<Integer> needRemoveList = new ArrayList<>();
        for (Integer activityId : activityMap.keySet()) {
            GuaGiftsConfig config = CrossGuaGiftsConfigMgr.getConfig(activityId);
            if (config == null) {
                needRemoveList.get(activityId);
            }
            if (currentSecond > config.getActivityInfo().getEndShowTime() + 5 * DateHelper.MINUTE_SECONDS) {
                needRemoveList.add(activityId);
            }
        }

        synchronized (activityMap) {
            for (Integer activityId : needRemoveList) {
                activityMap.remove(activityId);
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        getLogger().info("reload gua activity start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.GuaGiftsActivity.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            reloadActivity(item);
        }
        getLogger().info("reload gua activity end");
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (Integer activityId : activityMap.keySet()) {
            CrossGuaGiftsActivity activity = activityMap.get(activityId);
            if (activity != null) {
                activity.save();
            }
        }

        return true;
    }

    public static void reloadActivity(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return;
        }
        int activityId = activityInfo.getActivityId();
        if (CrossGuaGiftsConfigMgr.getConfig(activityId) == null) {
            CrossGuaGiftsConfigMgr.reloadConfig(activityInfo);
        }

        CrossGuaGiftsActivity tempActivity = new CrossGuaGiftsActivity(activityInfo);
        activityMap.put(activityId, tempActivity);
    }

    public static CrossGuaGiftsActivity getActivity(int activityId) {
        CrossGuaGiftsActivity crossQixiActivity = activityMap.get(activityId);
        if (crossQixiActivity == null) {
            synchronized (activityMap) {
                crossQixiActivity = activityMap.get(activityId);
                if (crossQixiActivity == null) {
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    if (CrossGuaGiftsConfigMgr.getConfig(activityId) == null) {
                        CrossGuaGiftsConfigMgr.reloadConfig(activityInfo);
                    }

                    crossQixiActivity = new CrossGuaGiftsActivity(activityInfo);
                    activityMap.put(activityId, crossQixiActivity);
                }
            }
        }
        return crossQixiActivity;
    }

    public static void receiveBroadcastMsg(GuaGiftsProto.CrossGuaGiftsGiftBroadcastMsg resultMsg) {
        CrossGuaGiftsActivity activity = getActivity(resultMsg.getActivityId());
        if (activity == null) {
            return;
        }

        activity.receiveBroadcastMsg(resultMsg);
    }

    public static void loginGetBroadcastMsg(int activityId, long serverId, long userId, List<Long> excludeId) {
        CrossGuaGiftsActivity activity = getActivity(activityId);
        if (activity == null) {
            return;
        }

        activity.loginGetBroadcastMsg(serverId, userId, excludeId);
    }

    public static GuaGiftsProto.GuaGiftsGiftBroadcastMsg.Builder parseBroadcastMsg(GuaGiftsBroadcastData data) {
        GuaGiftsProto.GuaGiftsGiftBroadcastMsg.Builder builder = GuaGiftsProto.GuaGiftsGiftBroadcastMsg.newBuilder();
        builder.setActivityId(data.getActivityId());
        builder.setBroadcastId(data.getBroadcastId());
        builder.setLeftUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(data.getSendUserBaseInfo()));
        builder.setRightUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(data.getReceiveUserBaseInfo()));
        builder.setGiftId(data.getGiftId());
        builder.setContent(data.getContent());
        builder.setSendUserId(data.getSendUserId());
        builder.setSendTime(data.getTime());

        return builder;
    }

    public static GuaGiftsProto.CrossGetBroadcastRewardRespMsg.Builder getBroadcastReward(int activityId, long broadcastId) {
        GuaGiftsProto.CrossGetBroadcastRewardRespMsg.Builder builder = GuaGiftsProto.CrossGetBroadcastRewardRespMsg.newBuilder();
        builder.setRet(0);
        CrossGuaGiftsActivity activity = getActivity(activityId);
        if (activity == null) {
            return builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
        }

        return activity.getBroadcastReward(broadcastId);
    }
}
